import axios from 'axios'

export default {
    getData(end_id = 0, _url) {
        let arr = _url.split("&")
        let split_end_id = arr.pop()
        let url = arr.reduce((pre, cur, index, arr) => {
            return pre + "&" + cur
        })
        url = url + `&end_id=${end_id}`
        // console.log("完整url==",url)
        // let urlPix = 'https://hk4e-api.mihoyo.com'  ${urlPix}
        // let url = `/event/gacha_info/api/getGachaLog?authkey_ver=1&sign_type=2&auth_appid=webview_gacha&init_type=301&gacha_id=e432d80a79eb2a8953331fe921c7b6dcf25aa808&timestamp=1657669515&lang=zh-cn&device_type=pc&ext=%7b%22loc%22%3a%7b%22x%22%3a211.3956756591797%2c%22y%22%3a215.19854736328126%2c%22z%22%3a302.45538330078127%7d%2c%22platform%22%3a%22WinST%22%7d&game_version=CNRELWin2.8.0_R9000494_S8883030_D9085436&plat_type=pc&region=cn_gf01&authkey=Y82gt4RnnRFCTO3t4DjMiS%2fxihIQ5ef2fD7G4W0tNPzWS%2fwYqXsQNIbkQeU457VFN8wXj8kiPbIbon4orAA22R9Mq82tbwfOKIzfJbTkT%2fqFiGNsvQInopNMfdYQMhJqHKNoF8o3gcGNDSawnNtslCiK%2fH8ubEVvNEXqxQWw%2fmVFsHOn2AL9AUSAO93dkgOdSeUg4Ikwas9TV4PO1wm1Hj%2bvt8HvaGRbdhjRErg69gHZSgInco8BAtMuiqNPaRIijNDGbQ1%2f%2fyuM%2bjm4aOkHKZ8mhGnJIkZVZpe0JmEPdzBEeRBUpXh6LNMKaNTS7q8Nkosy124VmtKgKs69OBHMr%2bd6KT9B%2fKvtjRBGa8i6lUa%2fTx5JWF0W6QBfb0PT7Dudok5V%2bnMuDibXRCGLx0GRBFI6CUJFz0QkxWu%2fWCqcwnogR%2bceAxVA4Jhw4hMQco%2bM36LWQyx2VRGUZh7ju8wRlGNNnbJW1erfOajf7qoRLC0ck%2bsOFtt0afApCyUtHtYKjm0xURbtvbyBh1DFktfLAsCJKz7wIH8FXSrYTtwZYstOCmpUSWeLthThEaEY2XGml9cxNpaB05l60D8tQ0sOYflgEgXxA6SVF1VHKu8b1CDTfFrx8Ye3Qxz%2bEbT1weyLbhhsn3IzxOT6%2fSKVvXBtNlnXQvtyLZoQ2Po29PLRrm2Zue03GrCxiUjk%2f1vG3gQibsmYupou%2fBdcEFJWN2wnDLivHXqgc7ENM%2bFcILeEyLk7TYpSUXSNd9rwsr9CTE2XQovIwQlTUGAJynbm2xO1%2fqRWoNf1uNDDvXIvZ9u3DCwojVZzzVgI%2fRFRa6kn34zHPitYlAxr5LA4CA8oMbPwsvqddaHyJ54HuN8I7VBqFDuScdQeqhFMEP%2b1fN4FxpexJP4utBUEB8BZiBqQu9jbSXye1DB6dsxHXkCb3IiCgJy2VVwb%2bntOoDFYqZGh5akXqm7UmBlgHhaEQ0GNaJU5MEO6hGFGDlwMQUuHUigLgyf%2bjwtpHDM00DM4y1447z54&game_biz=hk4e_cn&gacha_type=301&page=1&size=30&end_id=${end_id}`
        return axios.get(url)
    },
    test(url) {
        let arr = url.split("&")
        let split_end_id = arr.pop()
        const _url = arr.reduce((pre, cur, index, arr) => {
            return pre + "&" + cur
        })
        console.log(_url)
    },
    getAllData: async function (vueData, url) {
        const sleep = (ms)=>{
            return new Promise(resolve => setTimeout(resolve,ms))
        }
        let tmepData = []
        try {
            let count = 0
            let res = await this.getData(0, url)
            count = res.data.data.list.length
            let end_id = res.data.data.list[count - 1].id

            count = 2

            tmepData.push(res.data.data.list)
            while (count > 0) {
                if (count !== 0) {
                    // console.log("开始循环,count==",count)
                    let resData = await this.getData(end_id, url)
                    await sleep(3000)
                    // console.log("最终数据为==",resData)
                    if(resData.data.data===null){
                        break
                    }
                    let yuanshen_list = resData.data.data.list
                    count = resData.data.data.list.length
                    // console.log("最后一次count==",count)
                    if (count === 0) {
                        break
                    }
                    end_id = resData.data.data.list[count - 1].id
                    // tmepData  = [...tmepData,...yuanshen_list]
                    tmepData.push(yuanshen_list)
                } else {
                    break
                }
            }
            vueData.resData = tmepData
        }catch (e) {
            console.error(e)
            vueData.resData = tmepData
            return tmepData
        }

        return tmepData
        // console.log("vueData.resData = ",vueData.resData)
    }

}
